package com.zxl.security.browser.handler;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@AllArgsConstructor
public class MyLogoutSuccessHandler implements LogoutSuccessHandler {
    private String logoutUrl;

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        log.info("用户退出成功！！！");
        if (StringUtils.isEmpty(logoutUrl)) {
            response.setContentType("application/json;charset=UTF-8");
            response.getWriter().write("用户退出成功");
        } else {
            response.sendRedirect(logoutUrl);
        }
    }
}
